import { Inject, Injectable, NestMiddleware } from '@nestjs/common';
import { NextFunction, Request, Response } from 'express';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { getReqMainInfo } from 'src/utils/request.util';
import { Logger } from 'winston';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  // 注入日志服务相关依赖
  constructor(
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {}
  use(req: Request, res: Response, next: NextFunction) {
    // 记录日志
    this.logger.info('route', {
      req: getReqMainInfo(req),
    });
    next();
  }
}

// export function logger(req: Request, next: NextFunction) {
//   console.log(`${new Date()} ${req.method} ${req.originalUrl} logger`);
//   next();
// }
